/**
 * 行政机构选择器
 */
import React from 'react';
import { TreeSelect } from 'antd';

const { TreeNode } = TreeSelect;

const CompanySelect = React.forwardRef(
    ({ value, onChange, dataSource = [], disabledKeys, treeCheckable = true, ...rest }, ref) => {
        const renderTreeNodes = (arr = []) =>
            arr.map(item => (
                <TreeNode
                    title={item.organizationName}
                    key={item.organizationCode}
                    value={item.organizationCode}
                    disabled={disabledKeys?.includes(item.organizationCode)}
                >
                    {item.children && item.children.length > 0 ? renderTreeNodes(item.children) : null}
                </TreeNode>
            ));

        return (
            <TreeSelect
                ref={ref}
                value={value}
                onChange={onChange}
                allowClear
                showSearch
                treeDefaultExpandAll
                dropdownStyle={{ maxHeight: 250, overflowY: 'visible' }}
                treeCheckable={treeCheckable}
                style={{ width: '100%' }}
                placeholder="请选择或输入"
                treeNodeFilterProp="title"
                getPopupContainer={triggerNode => triggerNode.parentElement}
                {...rest}
            >
                {renderTreeNodes(dataSource)}
            </TreeSelect>
        );
    }
);

export default CompanySelect;
